package com.keyidea.jfxui.portal.view.loginregister;

import com.keyidea.jfxui.core.config.context.SpringContextHelper;
import com.keyidea.jfxui.core.system.LoginService;
import com.keyidea.jfxui.core.util.MemStorage;
import com.keyidea.jfxui.core.util.YamlUtil;
import de.saxsys.mvvmfx.SceneLifecycle;
import de.saxsys.mvvmfx.ViewModel;
import de.saxsys.mvvmfx.utils.commands.Action;
import de.saxsys.mvvmfx.utils.commands.Command;
import de.saxsys.mvvmfx.utils.commands.DelegateCommand;
import de.saxsys.mvvmfx.utils.validation.*;
import javafx.application.Platform;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;

/**
 * 登录注册视图模型
 */
public class LoginRegisterViewModel implements ViewModel, SceneLifecycle {
    public static final String ON_VIEW_ADDEDA = "onViewAdded";
    private Command loginCommand;
    private SimpleStringProperty userName = new SimpleStringProperty("admin");
    private SimpleStringProperty passWord = new SimpleStringProperty("admin123");
    private SimpleStringProperty uuId = new SimpleStringProperty();
    private SimpleBooleanProperty success = new SimpleBooleanProperty(false);
    private FunctionBasedValidator userNameValidator;
    private ObservableRuleBasedValidator passwordValidator;
    private CompositeValidator formValidator;
    private SimpleStringProperty msg = new SimpleStringProperty();
    private SimpleStringProperty systemName = new SimpleStringProperty();
    private SimpleStringProperty copyright = new SimpleStringProperty();

    public void initialize() {

        loginCommand = new DelegateCommand(() -> new Action() {
            @Override
            protected void action() throws Exception {
                login();
            }
        }, true); //Async
        userNameValidator = new FunctionBasedValidator(
                userNameProperty(),
                input -> input != null && !input.toString().trim().isEmpty(),
                ValidationMessage.error("请输入用户名"));

        passwordValidator = new ObservableRuleBasedValidator();
        passwordValidator.addRule(passWordProperty().isNotEmpty(), ValidationMessage.error("请输入用户密码"));

        formValidator = new CompositeValidator();
        formValidator.addValidators(userNameValidator, passwordValidator, new ObservableRuleBasedValidator(loginCommand.runningProperty().not(), ValidationMessage.error("正在登录...")));
        userName.setValue(YamlUtil.getValueStr(MemStorage.CONFIG_PROPERTIES, "app", "username"));
        passWord.setValue(YamlUtil.getValueStr(MemStorage.CONFIG_PROPERTIES, "app", "password"));
        systemName.setValue(YamlUtil.getValueStr(MemStorage.CONFIG_PROPERTIES, "app", "name"));
        copyright.setValue(YamlUtil.getValueStr(MemStorage.CONFIG_PROPERTIES, "app", "copyright"));
    }

    @Override
    public void onViewAdded() {
        publish(ON_VIEW_ADDEDA);
    }

    @Override
    public void onViewRemoved() {

    }


    public String getUserName() {
        return userName.get();
    }

    public SimpleStringProperty userNameProperty() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName.set(userName);
    }

    public String getPassWord() {
        return passWord.get();
    }

    public SimpleStringProperty passWordProperty() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord.set(passWord);
    }

    public String getUuId() {
        return uuId.get();
    }

    public SimpleStringProperty uuIdProperty() {
        return uuId;
    }

    public void setUuId(String uuId) {
        this.uuId.set(uuId);
    }

    public String getMsg() {
        return msg.get();
    }

    public SimpleStringProperty msgProperty() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg.set(msg);
    }

    public boolean isSuccess() {
        return success.get();
    }

    public SimpleBooleanProperty successProperty() {
        return success;
    }

    public ValidationStatus getUserNameValidator() {
        return userNameValidator.getValidationStatus();
    }

    public ValidationStatus getPasswordValidator() {
        return passwordValidator.getValidationStatus();
    }

    public ValidationStatus getFormValidator() {
        return formValidator.getValidationStatus();
    }

    public Command getLoginCommand() {
        return loginCommand;
    }

    public String getSystemName() {
        return systemName.get();
    }

    public SimpleStringProperty systemNameProperty() {
        return systemName;
    }

    public String getCopyright() {
        return copyright.get();
    }

    public SimpleStringProperty copyrightProperty() {
        return copyright;
    }

    /**
     * 登录
     */
    public void login() {
        LoginService loginService = SpringContextHelper.getBean(LoginService.class);
        boolean login = loginService.login(userName.getValue(), passWord.getValue());

        Platform.runLater(() -> {
            msg.setValue(login ? "登录成功" : "登录失败");
        });

        if (login) {
            success.setValue(true);
        }
    }

}
